<?php
/***
	客户联系记录表，包括：
	1. 根据客户ID查询联系记录 get_by_client($client_id)
	
**/
class Contact_record_model extends CI_Model {
	
	public function __construct() {
		$this->load->database();
	}
	//联合查询所有记录
	public function get_all( ) {
		
		$sqlstring = "select contact_record.contact_date, contact_record.means, contact_record.status, contact_record.id, contact_record.comment, contact_record.c_status, contact_person.name as contact_name, clients.name as client_name, clients.contact_by as name 
			from contact_record
			left join contact_person on contact_record.cp_id = contact_person.id 
			left join clients on contact_person.client_id = clients.id 
			order by clients.id";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
	}
	
	//联合查询当前用户所有记录
	public function get_all_user_name($user_name ) {
		
		$sqlstring = "select contact_record.contact_date, contact_record.means, contact_record.status, contact_record.id, contact_record.comment, contact_record.c_status, contact_person.name as contact_name, clients.name as client_name, clients.contact_by as name 
			from contact_record
			left join contact_person on contact_record.cp_id = contact_person.id 
			left join clients on contact_person.client_id = clients.id
			where clients.contact_by = '".$user_name."' and contact_record.c_status = '1' 
			order by clients.id";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
	}
	
	
	//根据客户ID查询联系记录，要求cp_id->cp_name
	public function get_by_client($client_id) {
		
		$sqlstring = "select contact_record.*, contact_person.name as cp_name from contact_record 
			inner join contact_person on contact_record.cp_id = contact_person.id where contact_record.client_id = '".$client_id."'";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
	}
		
	//根据ID查询联系记录
	public function get_by_id($id) {
		
		$query = $this->db->get_where('contact_record', array('id' => $id));
		return $query->row_array();
	}
	
	//新增联系信息
	public function create () {
		
		$client_id = $this->input->post('client_id');
		
		$data = array(
			'cp_id' => $this->input->post('cp'),
			'means' => $this->input->post('means'),
			'client_id' => $client_id,
			'contact_date' => $this->input->post('contact_date'),
			'update_date' => $this->input->post('contact_date'),
			'status' => $this->input->post('status'),
			'remark' => $this->input->post('remark')
		);
		
		$this->db->insert('contact_record', $data);
		//获取新增记录的id
		$id = $this->db->insert_id();
		//将这个id写入对应client的record1，而原来的record1移到record2
		$this->db->select('record1');
		$query = $this->db->get_where('clients', array('id' => $client_id));
		$row = $query->row();
		$record1 = $row->record1;
		
		$data2 = array(
			'record1' => $id,
			'record2' => $record1
		);
		//重置查询构造器
		$this->db->reset_query();
		$this->db->where('id', $client_id);
		$this->db->update('clients', $data2);
		
	}
	
	//修改联系信息
	//如果更新的记录已经在record1，将导致它被复制到record2
	public function update_all ($id, $update_date) {
		
		$client_id = $this->input->post('client_id');
		
		$data = array(
			'cp_id' => $this->input->post('cp'),
			'means' => $this->input->post('means'),
			'client_id' => $client_id,
			'contact_date' => $this->input->post('contact_date'),
			'update_date' => $update_date,
			'status' => $this->input->post('status'),
			'remark' => $this->input->post('remark')			
		);
		
		$this->db->where('id', $id);
		$this->db->update('contact_record', $data);
		//重置查询构造器
		$this->db->reset_query();
		
		//将$id写入对应client的record1，而原来的record1移到record2
		$this->db->select('record1');
		$query = $this->db->get_where('clients', array('id' => $client_id));
		$row = $query->row();
		$record1 = $row->record1;
		
		//只有$record1与$id不一致时才需要更新
		if ($id <> $record1) {
		$data2 = array(
			'record1' => $id,
			'record2' => $record1
		);
		//重置查询构造器
		$this->db->reset_query();
		$this->db->where('id', $client_id);
		$this->db->update('clients', $data2);
		}
	}
	
	//异步修改comments
	public function ajax_update_comment ($id, $comment) {
		$data = array(
			'comment' => $comment,
			'c_status' => 1
		);
		
		$this->db->where('id', $id);
		$this->db->update('contact_record', $data);
	}
	
	//异步修改c_status为2，已执行
	public function ajax_update_c_status ($id) {
		$data = array(
			'c_status' => 2
		);
		
		$this->db->where('id', $id);
		$this->db->update('contact_record', $data);
	}
	
	//删除联系记录
	public function del_by_id ($id) {
		$this->db->delete('contact_record', array('id' => $id));
		
	}
	
	//获取所有记录，用于输出csv文件
	public function get_all_csv( ) {
		
		$sqlstring = "select contact_record.means, contact_record.contact_date, contact_record.status, 
contact_person.name as contact_name, contact_person.mobile as contact_mobile, contact_person.tel as contact_tel, contact_person.email as contact_email, contact_person.id as contact_id, contact_person.position as contact_position, 
clients.name as client_name, clients.id as client_id, clients.country, clients.contact_by, clients.website, clients.address, clients.type as client_type, clients.scope from contact_person 
left join clients on contact_person.client_id = clients.id 
left join contact_record on contact_person.id = contact_record.cp_id 
order by clients.id";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
	}
	
	//获取对应销售员的所有记录，用于输出csv文件
	public function get_all_csv_sale( ) {
		$user_name = $_SESSION['user_name'];
		$sqlstring = "select contact_record.means, contact_record.contact_date, contact_record.status, 
contact_person.name as contact_name, contact_person.mobile as contact_mobile, contact_person.tel as contact_tel, contact_person.email as contact_email, contact_person.id as contact_id, contact_person.position as contact_position, 
clients.name as client_name, clients.id as client_id, clients.country, clients.contact_by, clients.website, clients.address, clients.type as client_type, clients.scope from contact_person 
left join clients on contact_person.client_id = clients.id 
left join contact_record on contact_person.id = contact_record.cp_id
where clients.contact_by = '".$user_name."' 
order by clients.id";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
	}
}